package com.lebang.service.earn;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lebang.common.utils.OkHttpUtil;
import com.lebang.component.RedisClient;
import com.lebang.domain.AppEarnNewsConfig;
import com.lebang.domain.MemberEarnLookRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.mapper.AppEarnNewsConfigMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.mapper.MemberEarnLookRecordMapper;
import com.lebang.model.NewsModel;
import com.lebang.model.NewsPage;
import com.lebang.model.Whether;
import com.lebangweb.common.TextUtil;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: hwq
 * @Date: 2020/3/10 11:38
 * @Version 1.0
 */
@Service
public class AppEarnLookService {
    @Autowired
    private AppEarnNewsConfigMapper appEarnNewsConfigMapper;
    @Autowired
    private MemberEarnLookRecordMapper recordMapper;
    @Autowired
    private RedisClient redisClient;
    private static final String BASE = "AppEarnLook:";
    @Autowired
    private LbbMemberMapper lbbMemberMapper;
    public AppEarnNewsConfig selectByKey(String key) {
        return appEarnNewsConfigMapper.selectByKey(key);
    }

    public MemberEarnLookRecord pull(RequestHeaders header, String xwz) {
        MemberEarnLookRecord record = new MemberEarnLookRecord();
        AppEarnNewsConfig vo = this.selectByKey(xwz);
        if (vo == null) {
            return null;
        }
        int i = RandomUtils.nextInt(0, 100);
        if (i <= vo.getDoubleCount()) {
            record.setIsDouble(Whether.YES.getDbIndex());
        } else {
            record.setIsDouble(Whether.NO.getDbIndex());
        }
        LocalDateTime time = lbbMemberMapper.selectCreaTimeByMemberId(header.getUserid());
        if (time != null) {
            if (LocalDate.now().equals(time.toLocalDate())) {
                record.setIsNewMember(Whether.YES.getDbIndex());
            } else {
                record.setIsNewMember(Whether.NO.getDbIndex());
            }
        }
        record.setIsDoubleStatus(Whether.NO.getDbIndex());
        record.setCreateTime(LocalDateTime.now());
        record.setMemberId(header.getUserid());
        record.setConfigId(vo.getId());
        record.setMsg(vo.getTitle());
        record.setPoint(vo.getPoint());
        record.setTradeTime(null);
        record.setStatus(0);
        record.setIpAddress(header.getIpAddr());
        record.setImei(header.getImei());
        record.setAppStore(header.getAppStore());
        record.setVersion(header.getVersion());
        record.setWaitTime(vo.getMinute());
        recordMapper.insertSelective(record);
        return record;
    }

    public String getKey(Integer type) {
        LocalDate date = LocalDate.now();
        return BASE + date.format(DateTimeFormatter.ISO_DATE) + ":" + type;
    }

    public MemberEarnLookRecord selectRecordById(Long id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    public boolean push(RequestHeaders header, MemberEarnLookRecord record) {
        record.setStatus(1);
        record.setTradeTime(LocalDateTime.now());
        redisClient.hincrby(getKey(record.getConfigId()), header.getUserid(), 1L);
        recordMapper.updateByPrimaryKeySelective(record);
        return true;
    }

    public boolean doubleCount(RequestHeaders header, MemberEarnLookRecord record) {
        record.setIsDoubleStatus(1);
        record.setPoint(record.getPoint() + record.getPoint());
        recordMapper.updateByPrimaryKeySelective(record);
        return true;
    }

    public int getCount(String xwz, String memberId) {
        AppEarnNewsConfig vo = this.selectByKey(xwz);
        if (vo == null) {
            return 0;
        }
        boolean on = redisClient.hexists(getKey(vo.getId()), memberId);
        Integer i = 0;
        if (!on) {
            redisClient.hset(getKey(vo.getId()), memberId, "0");
            redisClient.expire(getKey(vo.getId()), 86400);
        } else {
            String str = redisClient.hget(getKey(vo.getId()), memberId);
            i = Integer.parseInt(str);
        }
        return vo.getCount() - i;
    }
}
